function showSignUp() {
    $(".box").addClass("formshow")
}

function hideSignUp() {
    $(".box").removeClass("formshow")
}

//表单验证

//用户名验证
function userNameFocus() {
    var userNameId = $("#suName+span");
    userNameId.html("6-15位数字英文")
    userNameId.show()
}

/*当鼠标离开通行证用户名文本框时，提示文本及样式*/
function userNameBlur() {
    var userName = $("#suName").val();
    var userNameId = $("#suName+span");
    var reg = /[a-zA-Z]+|[\u4e00-\u9fa5]+/;
    if (userName == "") {
        userNameId.html("用户名不能为空");
        return false;
    }
    if (userName.length > 15) {
        userNameId.html("用户名长度过长")
        return false
    }
    if (userName.length < 6) {
        userNameId.html("用户名长度过短")
        return false
    }
    if (reg.test(userName) == false) {
        userNameId.html("至少包含英文");
        return false;
    }
    userNameId.html("<img src=''>");

    return true;
}

function findUserName() {
    if (userNameBlur()) {
        var username = $("#suName").val();
        $.ajax({
            type: "post",
            url: "/api/user/finduser",
            data: {
                username: username
            },
            success: function (e) {
                if (e.code != 200) {
                    var userNameId = $("#suName+span").html("用户名被注册");
                }
            },
            error: function (e) {
                console.log(e)
            },
            async: true
        });
    }
}

//表单验证

//密码验证
function userPasswordFocus() {
    var userPasswordId = $("#suPassword+span");
    userPasswordId.html("至少6位")
    userPasswordId.show()
}

/*当鼠标离开通行证用户名文本框时，提示文本及样式*/
function userPasswordBlur() {
    var userPassword = $("#suPassword").val();
    var userPasswordId = $("#suPassword+span");
    var reg = /[a-zA-Z0-9]{6,20}/;
    if (userPassword == "") {
        userPasswordId.html("密码不能为空");
        return false;
    }
    if (userPassword.length < 6) {
        userPasswordId.html("密码长度过短")
        return false
    }
    userPasswordId.html("<img src=''>");
    return true;
}

//密码验证
//密码确认验证
function passwordConfirm() {
    var confirm = $("#confirm").val();
    var confirmId = $("#confirm+span");
    var userPassword = $("#suPassword").val();
    if (confirm !== userPassword) {
        confirmId.html("两次密码不一致")
        return false
    }
    confirmId.html("<img src=''>");
    return true;
}

//密码确认验证
//教学号验证
function jluNameBlur() {
    var jluname = $("#jluname").val()
    var jlunameId = $("#jluname+span")
    var reg = /\d{8}/
    if (reg.test(jluname) == false) {
        jlunameId.html("请输入8位教学号")
        return false
    }
    jlunameId.html("")
    return true
}

//教学号验证

(function () {
    //登入模块
    $(".login").on("mouseenter", function () {
        $(".login-block").addClass("login-block-enter")
    })
    $(".login-block").parent().on("mouseleave", function () {
        $(".login-block").removeClass("login-block-enter")
    })
    $(".login-btn").on("click", function () {
        var username = $("#loginname").val()
        var password = $("#loginpassword").val()
        console.log(username,password)
        $.ajax({
            type: "post",
            url: "/api/user/login",
            data: {
                username: username,
                password: password
            },
            success: function (e) {
                if(e.code===200){
                    window.location.reload()
                }
                alert(e.message)
            },
            error: function (e) {

            },
            async: true
        })
        return
    })

    //注册模块
    var jluinfo = {}

    function pushUserInfo(data) {
        if (data.code == 200) {
            $("#stuName").html(data.name)
            $("#stuId").html(data.studId)
            $("#schName").html(data.schName)
            jluinfo.jluname = $("#jluname").val()
            jluinfo.jlupassword = $("#jlupassword").val()
        } else {
            $("#stuName").html("")
            $("#stuId").html("")
            $("#schName").html("")
            $("#jluname+span").html("教学号密码输入有误")
        }
    }

    //验证按钮
    $(".varifier").on("click", function () {
        var name = $("#jluname").val()
        var password = $("#jlupassword").val()
        console.log(name)
        $.ajax({
            type: "post",
            url: "/api/student/query/userinfo",
            data: {
                username: name,
                password: password
            },
            success: function (data) {
             pushUserInfo(data)
            },
            async: true
        });
    })
    $("#singUpButton").on("click", function () {
        var name = $("#suName").val();
        var password = $("#suPassword").val();
        var stuName = $("#stuName").html();
        var stuId = $("#stuId").html();
        var schName = $("#schName").html()
        if (stuName == "") {
            $(".verify-info").show()
            return false
        } else if (userNameBlur() && userPasswordBlur()) {
            $(".verify-info").hide()
            $.ajax({
                type: "post",
                url: "/api/user/register",
                data: {
                    username: name,
                    password: password,
                    jluname: jluinfo.jluname,
                    jlupassword: jluinfo.jlupassword,
                    stuName: stuName,
                    stuId: stuId,
                    schName: schName,
                },
                success: function (data) {
                    if (data.code === 200) {
                        alert(data.message)
                        hideSignUp()
                    }
                    else {
                        alert(data.massage)
                    }
                },
                async: true
            });
            return
        }
        //登入逻辑板块
    })
})()
